package android.slc.box.core.utils;

import android.slc.box.commonlibrary.compat.SlcPathUtils;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.util.Date;

public class ConsoleManager {

    private final PrintStream ps;

    public ConsoleManager() {
        ps = new ConsolePrint();
    }

    public void initialize() {
        System.setOut(ps);
        System.setErr(ps);
    }

    public static class ConsolePrint extends PrintStream {//可以正常解析utf8和gbk码

        public ConsolePrint() {
            super(new ByteArrayOutputStream());
        }

        @Override
        public void write(byte[] buf, int off, int len) {
            print(new String(buf, off, len));
        }

        @Override
        public void print(String s) {
            FileUtil.appendUtf8String(s, new File(SlcPathUtils.getAppCachePath(), DateUtil.format(new Date(), DatePattern.NORM_DATE_FORMAT)+".log"));
        }
    }

}
